import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import axios from "axios";
import VueAxios from "vue-axios";
import Http from "./untils/http";
import { Message } from "element-ui";

Vue.prototype.$request = Http;
Vue.prototype.$message = Message;
Vue.use(VueAxios, axios);
Vue.config.productionTip = false;
Vue.use(ElementUI);

//再次封装提示框
Vue.prototype.sucessMessage = (message) => {
  Message({
    message,
    type: "success",
    duration: 1000,
  });
};
Vue.prototype.errorMessage = (message) => {
  Message({
    message,
    type: "error",
    duration: 1000,
  });
};

router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
    next();
  } else {
    let token = sessionStorage.getItem("token");
    if (!token) {
      alert("您还没有登录，请先登录");
      next("/login");
    }
    next();
  }
});
//添加请求拦截器
axios.interceptors.request.use(function (config) {
  //在发送之前做些什么
  config.baseURL = "http://estate.eshareedu.cn/estate";
  config.headers["Authorization"] = sessionStorage.getItem("token");
  // config.headers = { Authorization: sessionStorage.getItem("token") }
  return config;
}),
  function (error) {
    //对请求错误做些什么
    return Promise.reject(error);
  };

//添加相应拦截器
axios.interceptors.response.use(
  function (response) {
    // console.log(response);
    if (response.status == 200) {
      if (response.data.errCode == 10000) {
        return response.data;
      } else {
        Message({
          message: response.data.errMsg,
          type: "error",
          duration: 1000,
        });
      }
    }
  },
  function (error) {
    return Promise.reject(error);
  }
);

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
